<?php

/**
 * reservas actions.
 *
 * @package    begurholidays
 * @subpackage reservas
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class reservasActions extends sfActions
{

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($reservas_alquileres = Doctrine_Core::getTable('ReservasAlquileres')->find(array($request->getParameter('id'))), sprintf('Object reservas_alquileres does not exist (%s).', $request->getParameter('id')));
    $this->form = new ReservasAlquileresForm($reservas_alquileres);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  
  public function executeLogin(sfWebRequest $request)
  {
  	sfProjectConfiguration::getActive()->loadHelpers(array('I18N'));
  	//$this->setLayout(false);
  	$this->form = new ReservasLoginForm();
  
  	if($request->isMethod("post")){
  		$this->form->bind($request->getParameter("login"));
  
  		if($this->form->isValid()){
  			if(!$reserva = ReservasAlquileresTable::login($this->form->getValue("order"), $this->form->getValue("id"))){
  				// No hemos conseguido loguear al usuario
  				// Redirigimos de nuevo al login con un mensaje de error
  				$this->getUser()->setFlash("errorNumReserva", __("numeroReservaNoExistente"));
  				//$this->form->getErrorSchema()->addError('datos incorrecots', ‘reserva’);
  				//$this->setLayout('layoutLogin');
  				$this->redirect("@reservaslogin_".$this->getUser()->getLangCulture());
  			}else{
  				// Logueamos
  				$this->getUser()->setAuthenticated(true);
  				$this->getUser()->setAttribute("id",$reserva->id);
  
  				// Lo redirigimos a la pagina de introduccion de datos de su reserva
  				$url = "@reservashow_".$this->getUser()->getLangCulture()."?id=".$reserva->id."&dsorder=".$this->form->getValue("order");
  				//$this->getUser()->setAttribute("referer", false);
  				//$this->setLayout('layoutLogin');
  				$this->redirect($url);
  			}
  		}
  	}
  	$this->getResponse()->setTitle("Costa Brava's");
  }
  
  public function executeLogout(sfWebRequest $request){
  	$this->getUser()->setAuthenticated(false);
  	$this->getUser()->getAttributeHolder()->clear();
  	$this->redirect("@reservaslogin_".$this->getUser()->getLangCulture());
  }
  
  public function executeShow(sfWebRequest $request){
  	sfProjectConfiguration::getActive()->loadHelpers(array('I18N'));
  	$q = Doctrine_Query::create()->from('ReservasAlquileres r')->where('r.id = ?', $request->getParameter('id'))->andWhere('r.dsorder = ?', $request->getParameter('dsorder'));
  	$this->reserva = $q->fetchOne();
  	$this->forward404Unless($this->reserva);
  	$this->getResponse()->setTitle(__("suReservaTitle", array('%0%' => $this->reserva->getViviendasAlquileres()->getFullName($this->reserva->getViviendasAlquileres())))." | Costa Brava's");
  }
  
  public function executeEditPersona(sfWebRequest $request){
  	sfProjectConfiguration::getActive()->loadHelpers(array('I18N'));
  	$q = Doctrine_Query::create()->from('ReservasPersonas p')->where('p.id = ?', $request->getParameter('id'))->andWhere('p.ReservasAlquileres.dsorder = ?', $request->getParameter('dsorder'));
  	$this->persona = $q->fetchOne();
  	
  	//Obtenemos info contextual de la reserva
  	$q = Doctrine_Query::create()->from('ReservasAlquileres r')->where('r.dsorder = ?', $request->getParameter('dsorder'));
  	$this->reserva = $q->fetchOne();
  	$this->forward404Unless($this->reserva);
  	
  	$this->form = new ReservasPersonasRegistroForm($this->persona);

  	$this->forward404Unless($this->persona);
  }
  
  public function executeUpdatePersona(sfWebRequest $request){
  	sfProjectConfiguration::getActive()->loadHelpers(array('I18N'));
  	$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
  	$q = Doctrine_Query::create()->from('ReservasPersonas p')->where('p.id = ?', $request->getParameter('id'))->andWhere('p.ReservasAlquileres.dsorder = ?', $request->getParameter('dsorder'));
  	$this->persona = $q->fetchOne();
  	
  	//Obtenemos info contextual de la reserva
  	$q = Doctrine_Query::create()->from('ReservasAlquileres r')->where('r.dsorder = ?', $request->getParameter('dsorder'));
  	$this->reserva = $q->fetchOne();
  	$this->forward404Unless($this->reserva);
  	
  	$this->forward404Unless($this->persona);
  	$this->form = new ReservasPersonasRegistroForm($this->persona);
  	
  	$this->processForm($request, $this->form, null);

  	
  	$this->setTemplate('editPersona');
  }
  
  
  public function executeNewPersona(sfWebRequest $request)
  {
  	sfProjectConfiguration::getActive()->loadHelpers(array('I18N'));
  	$q = Doctrine_Query::create()->from('ReservasAlquileres r')->where('r.dsorder = ?', $request->getParameter('dsorder'));
  	$this->reserva = $q->fetchOne();
  	$this->forward404Unless($this->reserva);
  	 
  	$this->form = new ReservasPersonasRegistroForm();
  }
  
  public function executeCreatePersona(sfWebRequest $request)
  {
  	
  	$this->forward404Unless($request->isMethod(sfRequest::POST));
  	$this->form = new ReservasPersonasRegistroForm();
  	
  	$f = $request->getParameter('reservaspersonasregistro');
  	
  	//Obtenemos info contextual de la reserva
  	$q = Doctrine_Query::create()->from('ReservasAlquileres r')->where('r.id = ?', $f['reserva_id']);
  	$this->reserva = $q->fetchOne();
  	$this->forward404Unless($this->reserva);
  	
  	$this->processForm($request, $this->form, $f['reserva_id']);
  	
  	$request->setParameter('reserva_id', $f['reserva_id']);
  	$request->setParameter('dsorder', $f['dsorder']);
  	
  	$this->setTemplate('newPersona');
  }

  public function executeDeletePersona(sfWebRequest $request)
  {
  	//$request->checkCSRFProtection();
  
  	//$this->forward404Unless($reservas_alquileres = Doctrine_Core::getTable('ReservasPersonas')->find(array($request->getParameter('id'))), sprintf('Object reservas_alquileres does not exist (%s).', $request->getParameter('id')));
  	$q = Doctrine_Query::create()->from('ReservasPersonas p')->where('p.id = ?', $request->getParameter('id'))->andWhere('p.ReservasAlquileres.dsorder = ?', $request->getParameter('dsorder'));
  	$this->persona = $q->fetchOne();
  	$this->forward404Unless($this->persona);
  	$reservaId = $this->persona->getReservaId();
  	$this->persona->delete();
  
  	// Lo redirigimos a la pagina de introduccion de datos de su reserva
  	$url = "@reservashow_".$this->getUser()->getLangCulture()."?id=".$reservaId."&dsorder=".$request->getParameter('dsorder');
  	//$this->getUser()->setAttribute("referer", false);
  	//$this->setLayout('layoutLogin');
  	$this->redirect($url);
  	
  	//$this->redirect('reservas/index');
  }
  
  protected function processForm(sfWebRequest $request, sfForm $form, $reserva_id){
  	sfProjectConfiguration::getActive()->loadHelpers(array('I18N'));
  	$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
  	if ($form->isValid()){
  		$reservas_persona = $form->save();
  		if($reserva_id){
  			$reservas_persona->setReservaId($reserva_id);
  			$reservas_persona->save();
  		}
  		$request->setParameter('reserva_id', $reserva_id);
  		//echo $reservas_persona->getReservaId();
  		$this->getUser()->setFlash('saved', 'ok');
  		$this->redirect('reservas/editPersona?id='.$reservas_persona->getId().'&dsorder='.$reservas_persona->getReservasAlquileres()->getDsorder());
  	}
  	
  }
  
}
